﻿using System.Web;
using CodeSystem.Models;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web.Mvc;
using Newtonsoft.Json;

namespace CodeSystem.Controllers
{
    [App_Start.CheckLogin]
    public class HomeController : Controller
    {
       
        public ActionResult Index()
        {
            return View();
        }
        public ActionResult Add()
        {
            return View();
        }
        [HttpPost]
        public JsonResult Upload()
        {
            if (Request.Files.Count > 0)
            {
                if (Request.Files.Count == 1)
                {
                    HttpPostedFileBase file = Request.Files[0];
                    if (file.ContentLength > 0)
                    {
                        string title = string.Empty;
                        title = DateTime.Now.ToString("yyyyMMddHHmmss") + "_" + Path.GetFileName(file.FileName);
                        string path = "/UploadFile/" + title;
                        string serverpath = System.Web.HttpContext.Current.Server.MapPath(path);
                        file.SaveAs(serverpath);
                        return Json(new { code = 0, msg = "",data=new { src =path, title= Path.GetFileName(file.FileName)}}) ;
                    }
                }
                else
                {
                    string[] urllist = new string[Request.Files.Count];
                    for (int i = 0; i < Request.Files.Count; i++)
                    {
                        HttpPostedFileBase file = Request.Files[i];
                        if (file.ContentLength > 0)
                        {
                            string title = string.Empty;
                            title = DateTime.Now.ToString("yyyyMMddHHmmss") + "_" + Path.GetFileName(file.FileName);
                            string path = "../UploadFile/" + title;
                            string serverpath = System.Web.HttpContext.Current.Server.MapPath(path);
                            file.SaveAs(serverpath);
                            urllist[i] = path;
                        }
                    }
                    return Json(new { code = 0, msg = "", data = new { src = urllist, title = "" } });
                }
            }
            else
            {
                return Json(new { code = 1, data = new { src = "", title = "" }, msg = "没有文件" });
            }
            return Json(new { code = 1,data = new { src = "", title = "" }, msg = "" });
        }
        [HttpPost,ValidateInput(false)]
        public JsonResult AddCode(string modelJson)
        {
            var model = JsonConvert.DeserializeObject<CodeMd>(modelJson);
            using (var db=new EfDbContext())
            {
                db.CodeModels.Add(model);
                if (db.SaveChanges()> 0){
                    return Json(new { code = 0 });
                }
                else
                {
                    return Json(new { code = 1 });
                }
            }
        }
        [HttpGet]
        public JsonResult GetCodes(int page,int limit)
        {
            using (var db=new EfDbContext())
            {
                var list = db.CodeModels.ToList();
                var codes = list.OrderByDescending(x => x.Id).Skip((page - 1) * limit).Take(limit).ToList();
                return Json(new { 
                    code=0,
                    msg="",
                    data=codes,
                    count=list.Count()
                },JsonRequestBehavior.AllowGet);
            }
        }
        [HttpGet]
        public JsonResult SearchCodes(string text)
        {
            using (var db = new EfDbContext())
            {
                var list = db.CodeModels.Where(x=>x.KeyWords.Contains(text)).ToList();
                return Json(new
                {
                    code = 0,
                    msg = "",
                    data = list,
                    count = list.Count()
                }, JsonRequestBehavior.AllowGet);
            }
        }
        [HttpGet]
        public JsonResult SetShow(int id)
        {
            using (var db = new EfDbContext())
            {
                var model = db.CodeModels.Find(id);
                model.IsShowed = !model.IsShowed;
                db.SaveChanges();
                return Json(new
                {
                    code = 0
                }, JsonRequestBehavior.AllowGet);
            }
        }
        [HttpGet]
        public JsonResult SetValue(int id)
        {
            using (var db = new EfDbContext())
            {
                var model = db.CodeModels.Find(id);
                model.IsValued = !model.IsValued;
                db.SaveChanges();
                return Json(new
                {
                    code = 0
                }, JsonRequestBehavior.AllowGet);
            }
        }
        [HttpGet]
        public JsonResult DelCode(int id)
        {
            using (var db = new EfDbContext())
            {
                var model = db.CodeModels.Find(id);
                db.CodeModels.Remove(model);
                db.SaveChanges();
                return Json(new
                {
                    code = 0
                }, JsonRequestBehavior.AllowGet);
            }
        }
        [HttpGet]
        public JsonResult GetCode(int id)
        {
            using (var db = new EfDbContext())
            {
                var model = db.CodeModels.Find(id);
                return Json(new
                {
                    model=model,
                    code = 0
                }, JsonRequestBehavior.AllowGet);
            }
        }
    }
}
